package com.sig.formes;

import java.util.ArrayList;
import java.util.List;

public class Triangle extends Forme{

	public Point p0;
	public Point p1;
	public Point p2;

	public Triangle(Point p0,Point p1,Point p2)	{
		this.p0 = p0;
		this.p1 = p1;
		this.p2 = p2;
	}

	public String toString(){
		return p0.toString()+" "+p1.toString()+" "+p2.toString();
	}
	
	public boolean contains(Point p)
	{

		if(     ( determinant( p0, p1, p ) > 0 )
				&&      ( determinant( p1, p2, p ) > 0 )
				&&      ( determinant( p2, p0, p ) > 0 )
				)
		{
			return true;
		}
		if(     ( determinant( p0, p1, p ) < 0 )
				&&      ( determinant( p1, p2, p ) < 0 )
				&&      ( determinant( p2, p0, p ) < 0 )
				)
		{
			return true;
		}
		return false;

	}

	public static double determinant(Point p0,Point p1,Point p2 )
	{
		return ( ( p0.x-p1.x )*( p2.y - p1.y ) ) - ( ( p2.x-p1.x )*( p0.y-p1.y ) );
	}

	public Point getCenter()
	{
		return new Point( ( p0.x + p1.x + p2.x ) / 3, ( p0.y + p1.y + p2.y ) / 3 );
	}




	/////////////////////////// OPENGL ///////////////////////

	// number of coordinates per vertex in this array
	static final int COORDS_PER_VERTEX = 3;

	public float[] getTriangleCoords(){
		float[] triangleCoords = new float[9];

		triangleCoords[0] = p0.getOpenGLX();
		triangleCoords[1] = p0.getOpenGLY();
		triangleCoords[2] = 0;

		triangleCoords[3] = p1.getOpenGLX();
		triangleCoords[4] = p1.getOpenGLY();
		triangleCoords[5] = 0;

		triangleCoords[6] = p2.getOpenGLX();
		triangleCoords[7] = p2.getOpenGLY();
		triangleCoords[8] = 0;

		return triangleCoords;
	}

	@Override
	public List<Triangle> getTriangles() {
		ArrayList<Triangle> a = new ArrayList<Triangle>();
		a.add(this);
		return a;
	}
	
	
}